package org.scboy.dbms.service.imp;

import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.BaseDictionaryTypeDao;
import org.scboy.dbms.dao.BaseDictionaryTypeJoinDao;
import org.scboy.dbms.dao.BaseDictionaryTypeValueDao;
import org.scboy.dbms.service.DictionaryTypeService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.lang.UUID;

/**
 * 字典业务类
 * @author yanghao
 *
 */
@Service
public class DictionaryTypeServiceImpl implements DictionaryTypeService {

	@Autowired
	EntityFactory factory;
	
	/**
	 * 分页查询字典
	 * @param model 查询参数
	 * @return 分页结果
	 */
	public Page page(Model model){
		
		BaseDictionaryTypeDao dao=factory.getDao(BaseDictionaryTypeDao.class);
		return dao.page(model);
	}
	
	public List<Model> list(Model model){
		
		BaseDictionaryTypeDao dao=factory.getDao(BaseDictionaryTypeDao.class);
		model.add("activity", true);
		return dao.select(model);
	}
	
	/**
	 * 新增字典
	 * @param model
	 * @return
	 */
	@Transactional
	public boolean add(Model model){
		BaseDictionaryTypeDao dao=factory.getDao(BaseDictionaryTypeDao.class);
		BaseDictionaryTypeJoinDao joinDao=factory.getDao(BaseDictionaryTypeJoinDao.class);
		String [] codes=model.getStringArray("code");
		for (String code : codes) {
			Model join=Model.create();
			join.add("id", UUID.randomUUID().toString());
			join.add("code", code);
			join.add("typeCode", model.getString("typeCode"));
			joinDao.insert(join);
		}
		if(dao.count(Model.create().add("typeCode", model.getString("typeCode")))>0){
			return false;
		}
		if(dao.insert(model)>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 更新字典
	 * @param model
	 * @return
	 */
	@Transactional
	public boolean update(Model model){
		BaseDictionaryTypeDao dao=factory.getDao(BaseDictionaryTypeDao.class);
		BaseDictionaryTypeJoinDao joinDao=factory.getDao(BaseDictionaryTypeJoinDao.class);
		String [] codes=model.getStringArray("code");
		//删除掉已存在的，重新新增
		joinDao.delete(Model.create().add("typeCode", model.getString("typeCode")));
		for (String code : codes) {
			Model join=Model.create();
			join.add("id", UUID.randomUUID().toString());
			join.add("code", code);
			join.add("typeCode", model.getString("typeCode"));
			joinDao.insert(join);
		}
		if(dao.update(model)>0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 删除字典
	 * @param id
	 * @return
	 */
	@Transactional
	public boolean delete(String typeCode){
		BaseDictionaryTypeDao dao=factory.getDao(BaseDictionaryTypeDao.class);
		BaseDictionaryTypeJoinDao joinDao=factory.getDao(BaseDictionaryTypeJoinDao.class);
		BaseDictionaryTypeValueDao valueDao=factory.getDao(BaseDictionaryTypeValueDao.class);
		
		joinDao.delete(Model.create().add("typeCode", typeCode));
		valueDao.delete(Model.create().add("typeCode", typeCode));
		int count= dao.delete(Model.create().add("typeCode", typeCode));
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}
	/**
	 * 获取字典
	 * @param code
	 * @return
	 */
	public Model get(String typeCode){
		BaseDictionaryTypeDao dao=factory.getDao(BaseDictionaryTypeDao.class);
		BaseDictionaryTypeJoinDao joinDao=factory.getDao(BaseDictionaryTypeJoinDao.class);
		
		List<Model> coedList=joinDao.select(Model.create().add("typeCode", typeCode));
		String [] codes=new String[coedList.size()];
		for(int i=0;i<coedList.size();i++) {
			codes[i]=coedList.get(i).getString("code");
		}
		Model model=dao.get(Model.create().add("typeCode", typeCode));
		model.add("code", codes);
		return model;
	}
	
	
	/**
	 * 启用禁用字典
	 * @param id ID
	 * @return
	 */
	public boolean checkState(String typeCode) {
		BaseDictionaryTypeDao dao=factory.getDao(BaseDictionaryTypeDao.class);
		Model model = get(typeCode);
		if (model == null) {
			return false;
		}
		model.add("activity", !model.getBoolean("activity"));
		int resultCount = dao.update(model);
		if (resultCount > 0) {
			return true;
		} else {
			return false;
		}
	}
}


